﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ProjetPacman;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace pacman
{
    class Pacman : Personnage
    {
        public const int DEAD = 5;

        public void reset(int niveau)
        {
            if (niveau == 1)
            {
                Direction = DROITE;
                NextDirection = DROITE;
                Position = new Vector2(13f, 23f);
            }
        }

        public Pacman(ContentManager content, int niveau)
            : base (niveau)
        {
            _vitesse = NORMAL;
            _vulnerable = true;

            reset(niveau);

            Texture2D image = content.Load<Texture2D>("Images/pacman");

            for (int i = 0; i < 4; i++)
            {
                Animation a = new Animation();
                
                a.AddFrame(new Frame(image, new Rectangle(20, i * 20, 20, 20), 250));
                a.AddFrame(new Frame(image, new Rectangle(0, i * 20, 20, 20), 250));
                a.AddFrame(new Frame(image, new Rectangle(40, 0, 20, 20), 250));
                a.AddFrame(new Frame(image, new Rectangle(0, i * 20, 20, 20), 250));
                animations.Add(a);
            }

            image = content.Load<Texture2D>("Images/death");
            Animation b = new Animation();
            for (int i = 0; i < 10; i++)
            {
                b.AddFrame(new Frame(image, new Rectangle(20*i, 0, 20, 20), 250));
            }
            animations.Add(b);

        }

        public int Eat(Levels level)
        {
            int score=0;
            int res = level.eatBlock(Position);
            if (res%4 == Levels.BEAN) score+= 20;
            else if (res%4 == Levels.PACGUM) score+= 50;
            if (res > 4) score += 100;

            return score;
        }

        public int Update(GameTime gameTime, Levels level)
        {
            KeyboardState keyboard = Keyboard.GetState();
            Vector2 nextBlock;
            if (keyboard.IsKeyDown(Keys.Right)) NextDirection = DROITE;
            else if (keyboard.IsKeyDown(Keys.Left)) NextDirection = GAUCHE;
            else if (keyboard.IsKeyDown(Keys.Down)) NextDirection = BAS;
            else if (keyboard.IsKeyDown(Keys.Up)) NextDirection = HAUT; 

            nextBlock = NextBlock(NextDirection);
            if (!finAnimation()||level.collision((int)nextBlock.X, (int)nextBlock.Y))//Animation non finie ou nouvelle direction impossible
            {
                nextBlock = NextBlock(Direction);
                if (!level.collision((int)nextBlock.X, (int)nextBlock.Y))//On ne rentre pas dans un mur
                {
                    Next(Direction);
                }
            }
            else // Animation finie, changement de direction autorisé ou continuation dans la même direction
            {
                Direction = NextDirection;
                animations.Start(Direction);
                Next(NextDirection);
            }

            return Eat(level);
        }
    }
}
